/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import Persistence.UserFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
import servidorped.Conexion;
import servidorped.User;

/**
 *
 * @author Micosil
 */
public class register implements Command {

    @Override //register Username RealName pass1 pass2 email
    public boolean execute(String[] stringlist, User user) {
        String pass1 = stringlist[3];
        String pass2 = stringlist[4];
        Boolean passwordMatch = pass1.equals(pass2);
        if (!passwordMatch) {
            user.getUserSender().setToSend("The passwords do not match.");
            return false;
        } else {
            User newUser = new User(user.getUserSocket(), stringlist[1], stringlist[2], stringlist[3], stringlist[5], "false");
            
            UserFactory.getUserPersistence().createUser(newUser);
            user.loginAs(newUser);
            servidorped.UserListFactory.getList().connectUser(user);
            Logger.getGlobal().log(Level.INFO, "User " + user.getUserName() + " has registered.");
            user.getUserSender().setToSend("You have been identified as " + user.getUserName());
            return true;
        }
    }
}